package yagwl.client.widget;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Widget;

/**
 * Lays out page in top-bottom-left-center-right format.
 * <p>
 * <b>CSS styles</b><br>
 * .ya-pageLayout<br>
 * .ya-pageLayout-globalHeader<br>
 * .ya-pageLayout-globalFooter<br>
 * .ya-pageLayout-header<br>
 * .ya-pageLayout-footer<br>
 * .ya-pageLayout-leftColumn<br>
 * .ya-pageLayout-centerColumn<br>
 * .ya-pageLayout-rightColumn
 */
public class PageLayout extends Composite {

  private FlexTable rootFT = new FlexTable();
  private Widget globalHeader;
  private Widget globalFooter;
  private Widget header;
  private Widget footer;
  private Widget leftColumn;
  private Widget centerColumn;
  private Widget rightColumn;
  private String leftColumnWidth = "240px";
  private String centerColumnWidth = "100%";
  private String rightColumnWidth = "240px";
  private int centerColRow;
  private int centerColCol;
  private boolean rendered = false;

  public PageLayout() {
    rootFT.setCellPadding(0);
    rootFT.setCellSpacing(0);
    initWidget(rootFT);
    setStyleName("ya-pageLayout");
  }

  public PageLayout(String leftColumnWidth, String rightColumnWidth) {
    this();
    this.leftColumnWidth = leftColumnWidth;
    this.rightColumnWidth = rightColumnWidth;
  }

  public void renderUI() {
    int cols = 0;
    if (leftColumn != null)
      cols++;
    if (centerColumn != null)
      cols++;
    if (rightColumn != null)
      cols++;
    if (cols == 0)
      cols = 1;
    int row = 0;
    if (globalHeader != null) {
      setupRow(row++, globalHeader, "ya-pageLayout-globalHeader", cols);
    }
    if (header != null) {
      setupRow(row++, header, "ya-pageLayout-header", cols);
    }
    int col = 0;
    if (leftColumn != null) {
      setupColumn(row, col++, leftColumn, "ya-pageLayout-leftColumn",
          leftColumnWidth);
    }
    if (centerColumn != null) {
      centerColRow = row;
      centerColCol = col;
      setupColumn(row, col++, centerColumn, "ya-pageLayout-centerColumn", null);
    }
    if (rightColumn != null) {
      setupColumn(row, col, rightColumn, "ya-pageLayout-rightColumn",
          rightColumnWidth);
    }
    row++;
    if (footer != null) {
      setupRow(row++, footer, "ya-pageLayout-footer", cols);
    }
    if (globalFooter != null) {
      setupRow(row, globalFooter, "ya-pageLayout-globalFooter", cols);
    }
    rendered = true;
  }

  private void setupRow(int row, Widget w, String style, int colSpan) {
    rootFT.setWidget(row, 0, w);
    rootFT.getCellFormatter().setStyleName(row, 0, style);
    rootFT.getFlexCellFormatter().setColSpan(row, 0, colSpan);
  }

  private void setupColumn(int row, int col, Widget w, String style,
      String width) {
    rootFT.setWidget(row, col, w);
    rootFT.getCellFormatter().setStyleName(row, col, style);
    if (width != null)
      rootFT.getCellFormatter().setWidth(row, col, width);
    rootFT.getCellFormatter().setVerticalAlignment(row, col,
        HasVerticalAlignment.ALIGN_TOP);
  }

  public void setGlobalHeader(Widget globalHeader) {
    this.globalHeader = globalHeader;
  }

  public void setGlobalFooter(Widget globalFooter) {
    this.globalFooter = globalFooter;
  }

  public void setHeader(Widget header) {
    this.header = header;
  }

  public void setFooter(Widget footer) {
    this.footer = footer;
  }

  public void setLeftColumn(Widget leftColumn) {
    if (!rendered) {
      this.leftColumn = leftColumn;
      return;
    }
    rootFT.setWidget(centerColRow, 0, leftColumn);
  }

  public void setCenterColumn(Widget centerColumn) {
    if (!rendered) {
      this.centerColumn = centerColumn;
      return;
    }
    rootFT.setWidget(centerColRow, centerColCol, centerColumn);
  }

  public void setRightColumn(Widget rightColumn) {
    this.rightColumn = rightColumn;
  }

}
